/**
 * 品牌
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @since 2018/11/10
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','jquery'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    layui.form.render();
    $("#hourFee").blur(function (){
        if(!checkValue($(this).val(),5)){
            $(this).val("");
            parent.layui.layer.msg("请输入大于5的正整数",{"icon":2});
        }
        totalPrice();
    });
    $("#trafficFee").blur(function (){
        if(!checkValue($(this).val(),5)){
            $(this).val("");
            parent.layui.layer.msg("请输入大于5的正整数",{"icon":2});
        }
        totalPrice();
    });
    $("#partFee").blur(function (){
        if(!checkValue($(this).val(),0)){
            $(this).val("0");
            parent.layui.layer.msg("请输入大于0的正整数",{"icon":2});
        }
        totalPrice();
    });
    $("#internalSettlementPrice").blur(function (){
        if(!checkValue($(this).val(),5)){
            $(this).val("");
            parent.layui.layer.msg("请输入大于5的正整数",{"icon":2});
        }
        totalPrice();
    });
    $("#externalSettlementPrice").blur(function (){
        if(!checkValue($(this).val(),5)){
            $(this).val("");
            parent.layui.layer.msg("请输入大于5的正整数",{"icon":2});
        }
        totalPrice();
    });
    function checkValue(value,minValue){
        var reg=/^[0-9]\d*$/; //由 0-9开头 的正则表达式
        //先判断是否为整数 在判断 是否在 1-总页 整数范围之内
        if(reg.test(value) && parseInt(value)>=minValue){
            return true;
        }else {
            return false;
        }
    }
    function totalPrice(){
        let total = 0;
        let  hourFee = $.trim($("#hourFee").val());
        let  trafficFee = $.trim($("#trafficFee").val());
        let  partFee = $.trim($("#partFee").val());
        if (hourFee!='' && !isNaN(hourFee)){
            total += parseFloat(hourFee);
        }
        if (trafficFee!='' && !isNaN(trafficFee)){
            total += parseFloat(trafficFee);
        }
        if (partFee!='' && !isNaN(partFee)){
            total += parseFloat(partFee);
        }
        $("#totalPrice").text(total);
        // $("#internalSettlementPrice").val(total);
        // $("#externalSettlementPrice").val(total);
    }
    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let data = obj.field;
        let hourFeeImpactfactor = new Array();
        let trafficFeeImpactfactor = new Array();
        let partFeeImpactfactor = new Array();
        let internalSettlementPriceImpactfactor = new Array();
        $("input[name=hourFeeImpactfactor]:checked").each(function(){
            hourFeeImpactfactor.push($(this).val());
        })
        $("input[name=trafficFeeImpactfactor]:checked").each(function(){
            trafficFeeImpactfactor.push($(this).val());
        })
        $("input[name=partFeeImpactfactor]:checked").each(function(){
            partFeeImpactfactor.push($(this).val());
        })
        $("input[name=internalSettlementPriceImpactfactor]:checked").each(function(){
            internalSettlementPriceImpactfactor.push($(this).val());
        })
        data.hourFeeImpactfactor = hourFeeImpactfactor.join(',');
        data.trafficFeeImpactfactor = trafficFeeImpactfactor.join(',');
        data.partFeeImpactfactor = partFeeImpactfactor.join(',');
        data.internalSettlementPriceImpactfactor = internalSettlementPriceImpactfactor.join(',');
        data.productId = json.productId;
        data.tariffId = json.serviceItem.tariffId;
        data.groupId = json.serviceItem.groupId;
        data.itemId = json.serviceItem.itemId;
        console.log(data);

        var url = "/product/tariff/tariffproductitem3/addEdit.action";

        $.ajax({
            async: false,
            type: "POST",
            url: url,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success:function(rs) {
                if (rs.status === 200) {
                    parent.layui.layer.msg(rs.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframeMsg(parent, rs.message);
                        parent.location.reload();
                    });
                }else {
                    parent.layui.layer.msg(rs.message,{"icon":2});
                    return false;
                }
            }
        })
        return false;
    });
    totalPrice();
    exports('serv_product_tariffproductitem3_setprice', {});
});

function reload(msg){
    parent.layui.layer.msg(msg);
    window.location.reload();
}
